package com.enlorenz.core.channel.socket.imp.longconnection.sendrecv;

import java.util.HashMap;
import java.util.Map;

import com.enlorenz.core.base.IReqResMessageHandle;
import com.enlorenz.core.channel.socket.common.reader.INioReader;
import com.enlorenz.core.channel.socket.common.reader.ISocketReader;
import com.enlorenz.core.channel.socket.common.translate.IMessageBytesTransformer;
import com.enlorenz.core.channel.socket.common.writer.INioWriter;
import com.enlorenz.core.channel.socket.common.writer.ISocketWriter;
import com.enlorenz.core.channel.socket.imp.longconnection.LongClientSocketChannel;
import com.enlorenz.core.channel.socket.imp.longconnection.NioLongServerSocketChannel;
import com.enlorenz.core.engine.AbstractEngine;
import com.enlorenz.core.queue.transfer.imp.WaitMessageQueue;

/**
 * Socket双通道链接
 * 先发后收
 * @author lcb
 */
public class LongSendRecvDoubleChannel<T> extends AbstractEngine implements IReqResMessageHandle<T>{
	//客户端通道
	private LongClientSocketChannel clientChannel;
	//服端通道
	private NioLongServerSocketChannel serverChannel;
	//客户端IP
	private String clientIp;
	//客户端口
	private int clientPort;
	//客户端的数据转换器
	private IMessageBytesTransformer<T> clientMessageBytesTransformer;
	//客户端队列
	private WaitMessageQueue<T> clientQueue;
	//客户端写出者
	private ISocketWriter clientWriter;
	//客户端读取者
	private ISocketReader socketReader;
	//服务端口
	private int serverPort;
	//服务等待数量
	private int backlog=10;
	//服务端写出者
	private INioWriter serverWriter;
	//服务端读取者
	private INioReader serverReader;
	//服务端端的数据转换器
	private IMessageBytesTransformer<T> serverMessageBytesTransformer;
	
	@Override
	public boolean isAlive() {
		return false;
	}

	@Override
	protected void initEngine() throws Exception {
		clientChannel=new LongClientSocketChannel();
		clientChannel.setIp(clientIp);
		clientChannel.setPort(clientPort);
		clientChannel.setMessageBytesTransformer(clientMessageBytesTransformer);
		clientChannel.setSendQueue(clientQueue);
		clientChannel.setSocketWriter(clientWriter);
		clientChannel.setSocketReader(socketReader);
		clientChannel.init();
		
		serverChannel=new NioLongServerSocketChannel();
		serverChannel.setBacklog(backlog);
		serverChannel.setListenPort(serverPort);
		serverChannel.setNioReader(serverReader);
		serverChannel.setNioWriter(serverWriter);
		serverChannel.setHandRecvThreadNum(5);
		serverChannel.setMessageBytesTransformer(serverMessageBytesTransformer);
		serverChannel.setRecvMsgHandle(new IReqResMessageHandle<T>() {
			@Override
			public T reqResMsgHandle(T message) {
				String serNo=getSerNo(message);
				recvDataMap.put(serNo, message);
				return null;
			}
		});
		serverChannel.init();
	}
	//接收数据Map
	private Map<String,T> recvDataMap=new HashMap<String,T>();
	
	/**
	 * 获取流水号
	 * @param message
	 * @return
	 */
	protected String getSerNo(Object message){
		return "1";
	}
	
	@Override
	public T reqResMsgHandle(T message) {
		//先发送消息
		clientQueue.addMsg(message);
		String serNo=getSerNo(message);
		
		long st=System.currentTimeMillis();
		while(true){
			T result=recvDataMap.get(serNo);
			if(null!=result){
				return result;
			}
			long ed=System.currentTimeMillis();
			if((ed-st)>3000){
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}

	
	@Override
	protected void startEngine() throws Exception {
		clientChannel.start();
		serverChannel.start();
	}

	@Override
	protected void shutdownEngine() throws Exception {
		clientChannel.shutdown();
		serverChannel.shutdown();
	}

}
